<template>
  <div id="user">
    <h2 class="h2">会员登入</h2>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="请输入邮箱" label-width prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <div id="button">
        <el-form-item>
          <el-button type="primary" @click="submitForm">登陆</el-button>
          <el-button @click="resetForm('ruleForm')">清空</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "login_user",
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('账号不能为空'));
      }
    };
    var validatePass = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入密码'));
      }
    };
    return {
      ruleForm: {
        pass: '',
        email: '',
      },
      rules: {
        pass: [
          {validator: validatePass, trigger: 'blur'}
        ],
        email: [
          {validator: checkAge, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm() {
      this.$http.get("http://localhost:8088/user/findByEmail", {
        xhrFields: {withCredentials: true},
        crossDomain: true,
        params: {
          pass: this.ruleForm.pass,
          email: this.ruleForm.email,
        }
      }).then(resp => {
        if (this.ruleForm.email == '' || this.ruleForm.pass == '') {
          this.$message({
            message: '请输入完整！',
            type: 'warning'
          });
        } else if (resp.data) {
          this.$http.get("http://localhost:8088/redis/visit").then(resp=>{
            console.log(resp.data)
            this.$router.push({
              name:'user',
              query:{
                email:this.ruleForm.email,
              }
            })
          })
        } else {
          console.log("错误")
          this.$message.error('账户名或密码错误！请重新输入！');
        }
      }).catch({})
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
#user {
  border: 1px solid white;
  padding-left: 0;
  padding-right: 60px;
  padding-top: 20px;
  margin: auto;
  width: 500px;
  margin-top: 100px;
  border-radius: 10px;
}

.h2 {
  margin-left: 50px;
  color: black;
}

</style>